<?php
/**
 * 获取各种MAP数据
 */
namespace App\Services\Tools;

use Illuminate\Support\Facades\Redis;

class MapService
{
    /**
     * 获取人脉阶级map
     */
    public static function connectionClassMap()
    {
        $redis = app('redis');
        $key = 'connection:class_list';
        $map = $redis->get($key);
        if (empty($map)) {
            $list = app('model_connection_class_config')
                ->select('title','icon_url','class_number','max_grade_number','day_intimacy_max','day_intimacy_free_max','type','product_id','mall_config','intimacy_addition','intimacy_increase_rate','diy_nickname','online_notice','joinroom_notice','mall_config')
                ->where('status', 1)
                ->orderBy('class_number', 'desc')->orderBy('id','desc')
                ->get()->toArray();
            if (empty($list)) {
                return [];
            }
            $map = [];
            foreach ($list as $value) {
                $map[$value['type']][$value['class_number']] = $value;
            }
            $redis->setex($key, 300, json_encode($map));
        } else {
            $map = json_decode($map, true);
        }
        return $map;
    }

    /**
     * 所有道具map
     */
    public static function allRewardItemMap()
    {
        $redis = app('redis');
        $key = 'map:all_reward_item_map';
        $map = $redis->get($key);
        if (empty($map)) {
            $list = app('model_reward_item')
                ->select('id','name','picture','type','active','sale','product_id')
                ->get()->toArray();
            if (empty($list)) {
                return [];
            }
            $map = array_column($list, null, 'id');
            $redis->setex($key, 300, json_encode($map));
        } else {
            $map = json_decode($map, true);
        }
        return $map;
    }

    /**
     * 所有道具包map
     */
    public static function allRewardMap()
    {
        $redis = app('redis');
        $key = 'map:all_reward_map';
        $map = $redis->get($key);
        if (empty($config)) {
            $list = app('model_reward')
                ->select('id','name','picture','type','number','active','sale')
                ->get()->toArray();
            if (empty($list)) {
                return [];
            }
            $map = array_column($list, null, 'id');
            $redis->setex($key, 300, json_encode($map));
        } else {
            $map = json_decode($map, true);
        }
        return $map;
    }

    /**
     * 所有礼物（包含已下架礼物）map
     */
    public static function allGiftMap()
    {
        $redis = app('redis');
        $key = 'map:all_gift_map';
        $map = $redis->get($key);
        if (empty($map)) {
            $list = app('model_gift')
                ->select('id','name','picture','price','special','type','sold','cardiac','frame_id')
                ->orderBy('price', 'desc')
                ->get()->toArray();
            if (empty($list)) {
                return [];
            }
            $map = array_column($list, null, 'id');
            $redis->setex($key, 300, json_encode($map));
        } else {
            $map = json_decode($map, true);
        }
        return $map;
    }

    /**
     * 礼物外框配置map
     */
    public static function giftFrameMap()
    {
        $redis = app('redis');
        $key = 'map:gift_frame_map';
        $map = $redis->get($key);
        if (empty($map)) {
            $frameList = app('model_gift_frame')->where(['deleted' => 0])
                ->select('id','condition1','condition2','condition3','frame1','frame2','frame3','default_score','one_star_score','two_star_score','three_star_score')
                ->get()->toArray();
            if (empty($frameList)) {
                return [];
            }
            $map = array_column($frameList, null, 'id');
            $redis->setex($key, 300, json_encode($map));
        } else {
            $map = json_decode($map, true);
        }
        return $map;
    }

    /**
     * 礼物分类map
     */
    public static function giftTypeMap()
    {
        $redis = app('redis');
        $key = 'map:gift_type_map';
        $map = $redis->get($key);
        if (empty($map)) {
            $list = app('model_gift_type')
                ->select('id','name')
                ->get()->toArray();
            if (empty($list)) {
                return [];
            }
            $map = array_column($list, null, 'id');
            $redis->setex($key, 300, json_encode($map));
        } else {
            $map = json_decode($map, true);
        }
        return $map;
    }

    /**
     * 礼物父级分类map
     */
    public static function giftTypeByParentMap()
    {
        $redis = app('redis');
        $key = 'map:gift_type_by_parent_map';
        $map = $redis->get($key);
        if (empty($map)) {
            $giftTypeList = app('model_gift_type')->select('id','parent_id')->get()->toArray();
            if (empty($giftTypeList)) {
                return [];
            }
            $map = [];
            foreach ($giftTypeList as $value) {
                if (!$value['parent_id']) {
                    $map[$value['id']] = [];
                }
            }
            foreach ($giftTypeList as $value) {
                if (isset($map[$value['parent_id']])) {
                    $map[$value['parent_id']][] = $value['id'];
                }
            }
            $redis->setex($key, 300, json_encode($map));
        } else {
            $map = json_decode($map, true);
        }
        return $map;
    }
    /**
     * 所有公会商城商品（包含已下架商品）map
     */
    public static function shopProductMap()
    {
        $redis = app('redis');
        $key = 'sc:shop_product_map';
        $map = $redis->get($key);
        if (empty($map)) {
            $list = app('model_sc_shop_product')
                ->select('id', 'name', 'img', 'category_id', 'property')
                ->orderBy('add_time', 'desc')
                ->get()->toArray();
            if (empty($list)) {
                return [];
            }
            $map = array_column($list, null, 'id');
            $redis->setex($key, 300, json_encode($map));
        } else {
            $map = json_decode($map, true);
        }
        return $map;
    }

    /**
     * 获取用户公会徽章
     */
    public static function userSociatyBadge($user_id)
    {
        /*用户所属公会*/
        $sociaty_user_res = app('model_sc_sociaty_user')->where(['user_id' => $user_id])->first();
        if (!$sociaty_user_res) {
            return [];
        }
        /*获取使用徽章id*/
        $shop_warehouse_res = Redis::get('sc:shop_warehouse:' . $sociaty_user_res->sociaty_id);
        if (!$shop_warehouse_res) {
            return [];
        }
        $shop_warehouse_res = json_decode($shop_warehouse_res, true);
        /* 所有公会商城商品（包含已下架商品）map*/
        $shopProductMap = self::shopProductMap();
        /*用户公会徽章*/
        $product_data = [
            'name' => $shop_warehouse_res['custom_name'] ?? '',
            'img' => $shopProductMap[$shop_warehouse_res['product_id']]['img'] ?? '',
        ];
        return $product_data;
    }
}
